package at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.arbeiter;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.communication.LokTopicQueueConnHandler;
import at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.teile.Lokomotive;


public class Logistik extends Arbeiter implements Runnable, MessageListener {
    
    private static int logistikId;
    
    private LokTopicQueueConnHandler lokTopicQueueConnHandler;
    private TopicSession topicSession;
    private TopicPublisher topicPublisher;
    
    private Lokomotive lokomotive = null;
    
    public Logistik() throws JMSException {
        super();
        topicSubscriber = getLokTopicQueueConnHandler().getTopSubscriber();
        topicSubscriber.setMessageListener(this);
        topicPublisher = getLokTopicQueueConnHandler().getTopPublisher();
       
    }
    
    private synchronized boolean isLokomotiveAvailable() {
        return lokomotive != null;
    }
    
    private synchronized TopicPublisher getTopicPublisher() {
        return this.topicPublisher;
    }
    
    
    
    private synchronized void publish() throws JMSException {
        ObjectMessage messageOut = getTopicSession().createObjectMessage();
        messageOut.setStringProperty("bauteil","Lokomotive");
        messageOut.setObject(lokomotive);
        topicPublisher.publish(messageOut);
        resetLokomotiveAvailability();
    }
    
    private  synchronized TopicSession getTopicSession() {
        return super.topicSession;
    }
    
    private synchronized void resetLokomotiveAvailability() {
        lokomotive = null;
    }
    
    private synchronized boolean isLokomotiveLackiert() {
        return lokomotive.isLackiert();
    }
    
    private synchronized boolean isLokomotiveAusgeliefert() {
        return lokomotive.isAusgeliefert();
        
    }

    public static void main(String[] args) {
        try {
           
            Random randomGenerator = new Random();
            logistikId = randomGenerator.nextInt(100);
            Logistik logistik = new Logistik();
            Thread t = new Thread(logistik);
            t.start();
        } catch (JMSException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    
    public void run() {
        
        System.out.println("Ich bin ein Logistiker mit der Nummer: " + logistikId);
   
        while (true) {
               
            while (true) {
                if (isLokomotiveAvailable()) {
                    if (isLokomotiveLackiert() == true && isLokomotiveAusgeliefert()==true) {
                        try {
                            System.out.println("Logistik -> Die Lok wird ausgeliefert!");
                            publish();
                        } catch (JMSException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }    
        }       
    }
    
    public void onMessage(Message message) {
        ObjectMessage oMsg = null;           
        try { 
          if (message instanceof ObjectMessage) {                
                oMsg = (ObjectMessage) message;
                if (oMsg.getStringProperty("bauteil").equals("Lokomotive")) {
                    lokomotive = (Lokomotive) oMsg.getObject();
                    if (lokomotive.isLackiert() == true && lokomotive.isAusgeliefert() == false) {                 
                        System.out.println("Logistiker -> Reading message: "
                                + ((Lokomotive) oMsg.getObject()).getName()
                                + ((Lokomotive) oMsg.getObject()).getId());
                        lokAusliefern(lokomotive);
                    }    
                }
            } else {
                System.out.println("Message of wrong type: " + message.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): " +
                e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():" +
                t.getMessage());
        }
    }
    
    public void lokAusliefern(Lokomotive lok) {
        lok.setAusgeliefert(true);
    }
    

}
